//
//  UnStartEventView.m
//  RunOneZhangLu
//
//  Created by runone02 on 17/3/8.
//  Copyright © 2017年 runone. All rights reserved.
//

#import "UnStartEventView.h"
#import "SDAutoLayout.h"
#import "YZInputView.h"

@interface UnStartEventView ()


@property(nonatomic,   copy) NSString      *leftTitleStr;
@property (nonatomic, assign) NSInteger textH;
//@property(nonatomic, copy) NSString *desStr;
@end

@implementation UnStartEventView

- (UnStartEventView *) initWithDefalutHeight:(CGFloat) height title:(NSString *)titleStr {
    _leftTitleStr = titleStr;
    return [self initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size .width- 130 - 16, height)];
}


-(instancetype)initWithFrame:(CGRect)frame {
    self=[super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor colorWithRed:226.0 / 255 green:230.0 / 255 blue:240.0 / 255 alpha:1]];
        
        [self configUI];
        [self layoutUI];
    }
    return self;
}

- (void) configUI {
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont systemFontOfSize:15];
    _titleLabel.text = _leftTitleStr;
    _titleLabel.textAlignment = NSTextAlignmentRight;
//    _titleLabel.backgroundColor = [UIColor blueColor];
    [self addSubview:_titleLabel];
    
    __weak __typeof(self)weakSelf = self;
    _desTextV = [[YZInputView alloc] init];
    _desTextV.backgroundColor  = [UIColor whiteColor];
    _desTextV.cornerRadius     = 0;
    _desTextV.font = [UIFont systemFontOfSize:15];
    _desTextV.userInteractionEnabled  = YES;
    _desTextV.maxNumberOfLines = 10;
    _desTextV.yz_textHeightChangeBlock = ^(NSString *text,CGFloat textHeight){
        if (text.length > 0 ) {
            [weakSelf updateMyLayout:textHeight];
//            weakSelf.desTextV.sd_layout
//            .leftSpaceToView(weakSelf.titleLabel, 0)
//            .topSpaceToView(weakSelf, 8)
//            .rightSpaceToView(weakSelf, 8)
//            //    .autoHeightRatio(0)
//            .heightIs(textHeight)
//            ;
        }
    };
    [self addSubview:_desTextV];
    
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_lineView];
    
}


- (void) layoutUI {
    _titleLabel.sd_layout
    .leftSpaceToView(self, 0)
    .topSpaceToView(self, 17)
    .widthIs(110)
    .autoHeightRatio(0)
    ;
    
    _desTextV.sd_layout
    .leftSpaceToView(self.titleLabel, 8)
    .rightSpaceToView(self, 8)
    .heightIs(34)
    .topSpaceToView(self, 8)
    //.bottomSpaceToView(self, 8)
    ;
    
    _lineView.sd_layout
    .leftSpaceToView(self, 8)
    .rightSpaceToView(self, 8)
    .heightIs(1)
    .topSpaceToView(self.desTextV, 8)
    ;
    
    [self setupAutoHeightWithBottomView:self.lineView bottomMargin:0];
}
#pragma mark -  设置
- (void) setContentString:(NSString *)contentStr {
//    contentStr = @"Audfudigdgjdggyghhocghfaioghdfaghagyrghdfjhgfihairghogharhghfajhg8ahgahdfgfaghrhgfhgareioghfdkhgriohgafgafhrhgfgfgjfghrihagrhhgfhroiahgfgrhighfghfghraogjjreaghoadghrdfdjgjrawoirgfghjgAB";
    self.desTextV.text = contentStr;
    CGFloat kScreen_Width = [UIScreen mainScreen].bounds.size.width - 110 - 8 - 8 ;
    CGFloat text = [contentStr boundingRectWithSize:CGSizeMake(kScreen_Width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height;
    CGFloat textHeight  = text + 8 + 8;
    [self updateMyLayout:textHeight];
}
- (void) updateMyLayout:(CGFloat )textHeight {
//    UIView *view = [_desTextV.text length] > [_titleLabel.text length] ? _desTextV : _titleLabel;
    
    if (_desTextV.text.length == 0) {
        textHeight = 34;
    }
    
    self.desTextV.sd_layout
    .leftSpaceToView(self.titleLabel, 8)
    .topSpaceToView(self, 8)
    .rightSpaceToView(self, 8)
    //    .autoHeightRatio(0)
    .heightIs(textHeight)
    ;
    [self layoutSubviews];
}




@end
